package com.roden.study.java.net;

import org.junit.Test;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

/**
 * @Author luodeng
 * @create 2021/9/6 16:24
 * 
 * URI表示一个 统一资源标识符 引用，Uniform Resource Identifiers
 *      http://java.sun.com/j2se/1.3/ 
 *      docs/guide/collections/designfaq.html#28 
 *      ../../../demo/jfc/SwingSet2/src/SwingSet2.java 
 *      file:///~/calendar
 * URL代表一个 统一资源定位符 ，Uniform Resource Locator，它是指向一个互联网 “ 资源 ” 的指针。互联网是关键，URL指向的资源都是网络资源，所以它需要指定一个具体的协议。
 *      protocol://userInfo@host:port/path?query#fragment
 *      http://www.example.com/docs/resource1.html
 *      
 * URI.toURL() 和 URL.toURI()
 * 
 * 
 */
public class UriTest {
    @Test
    public void main() throws Exception{
        URI uri = URI.create("http://www.baidu.com");        
        URLConnection urlConnection = uri.toURL().openConnection();       
        InputStream inputStream = urlConnection.getInputStream();
        InputStreamReader reader = new InputStreamReader(inputStream);
        char[] c = new char[1024];
        while (reader.read(c) != -1) {
            System.out.print(c);
        }      
    }

    @Test
    public void getDomainByUri() throws Exception {
        String str="https://www.gdcpo.com:8080/search?name=dog&age=22#roden-1";
        //str = URLEncoder.encode(str, "UTF-8");
        URI uri = URI.create(str);        
        URI newUri = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), -1, null, null, null);        
        System.out.println(newUri);
    }    
    
    @Test
    public void getDomainByUrl() throws URISyntaxException, MalformedURLException {
        String str="https://localhost:8080/search?name=dog&age=22#roden-1";
        URL url = new URL(str);       
        URL newUri = new URL(url.getProtocol(), url.getHost(), "");        
        System.out.println(newUri);
    }
    
    
}
